<?php

// no direct access
defined('_JEXEC') or die('Restricted access');

class ApkipasHelper {

	public static function subMenu() {
		$user = &JFactory::getUser();
		$view = JRequest::getCmd('view');
		$view = JString::strtolower($view);
		$params = & JComponentHelper::getParams('com_apkipas');
		JSubMenuHelper::addEntry('Items', 'index.php?option=com_apkipas&view=apkipas', $view == 'apkipas');
		JSubMenuHelper::addEntry('Categories', 'index.php?option=com_categories&extension=com_apkipas', $view == 'categories');
		
		JSubMenuHelper::addEntry('Resources', 'index.php?option=com_apkipas&view=resources', $view == 'resources');
		JSubMenuHelper::addEntry('Downloads', 'index.php?option=com_apkipas&view=downloads', $view == 'downloas');
	}	
	
	public static function getActions($categoryId = 0, $contactId = 0)
	{
		$user	= JFactory::getUser();
		$result	= new JObject;
	
		$actions = array(
				'core.admin', 'core.manage', 'core.create', 'core.edit', 'core.edit.own', 'core.edit.state', 'core.delete'
		);
	
		foreach ($actions as $action) {
			$result->set($action,	$user->authorise($action, $assetName));
		}
	
		return $result;
	}
	
	public static function setAlias($title,$alias){		
		jimport('joomla.filter.output');
		
		if($alias != '')
			$title = $alias;

		if (JString::trim($title) == '') {
			$this->setError(JText::_('K2_CATEGORY_MUST_HAVE_A_NAME'));
			return false;
		}

		if(JFactory::getConfig()->get('unicodeslugs') == 1) {
			$alias = JApplication::stringURLSafe($title);
		}
		else {
			mb_internal_encoding("UTF-8");
			mb_regex_encoding("UTF-8");
			$alias = trim(mb_strtolower($title));
			$alias = str_replace('-', ' ', $alias);
			$alias = str_replace('/', '-', $alias);
			$alias = mb_ereg_replace('[[:space:]]+', ' ', $alias);
			$alias = trim(str_replace(' ', '-', $alias));
			$alias = str_replace('.', '', $alias);
			$alias = str_replace('"', '', $alias);
			$alias = str_replace("'", '', $alias);
			 
			$stripthese = ',|~|!|@|%|^|(|)|<|>|:|;|{|}|[|]|&|`|â€ž|â€¹|â€™|â€˜|â€œ|â€�|â€¢|â€º|Â«|Â´|Â»|Â°|«|»|…';
			$strips = explode('|', $stripthese);
			foreach ($strips as $strip) {
				$alias = str_replace($strip, '', $alias);
			}
		}

		return $alias;
	}
	
	public static function getTag($id){
		$db = &JFactory::getDBO();
		if($id == 0)
			return false;
		$query = 'SELECT b.text FROM #__apkipas_refs as a 
			LEFT JOIN #__apkipas_tags as b ON b.id = a.tagid 
			WHERE a.id = '.$id;

		$db->setQuery($query);
		return $db->loadObjectList();
	}
	
	public static function getDownload($id){
		if($id == 0)
			return false;
		
		$db = &JFactory::getDBO();
		$query = 'SELECT * FROM #__apkipas_downloads WHERE product_id = '.$id.' ORDER BY version DESC';
		$db->setQuery($query);
		$rs = $db->loadObjectList();
		
		if(count($rs) > 0){
			foreach ($rs as $num =>  $row){
				$query = 'SELECT * FROM #__apkipas_download_detail WHERE download_id = '.$row->id.' ORDER BY id DESC';
				
				$db->setQuery($query);
				$lrs = $db->loadObjectList();
				if(count($lrs) > 0)
					$rs[$num]->link = $lrs;
			}
			
			return $rs;
		}
		else 
			return false;
	}
	
	// Word limit
	public static function wordLimit($str, $limit = 120, $end_char = '&#8230;') {
		if (trim($str) == '') return $str;
		$str = utf8_decode($str);
		// always strip tags for text
		$str = strip_tags($str);
	
		$find = array("/\r|\n/","/\t/","/\s\s+/");
		$replace = array(" "," "," ");
		$str = preg_replace($find,$replace,$str);
	
		preg_match('/\s*(?:\S*\s*){'.(int) $limit.'}/', $str, $matches);
		if (strlen($matches[0]) == strlen($str))
			$end_char = '';
		return rtrim($matches[0]).$end_char;
	}
	
	public static function characterLimit($str, $limit = 150, $end_char = '...') {
		if (trim($str) == '') return $str;
	
		$str = strip_tags(trim($str));
	
		$find = array("/\r|\n/","/\t/","/\s\s+/");
		$replace = array(" "," "," ");
		$str = preg_replace($find,$replace,$str);
	
		if(strlen($str)>$limit){
			if(function_exists("mb_substr")) {
				$str = mb_substr($str, 0, $limit);
			} else {
				$str = substr($str, 0, $limit);
			}
			return rtrim($str).$end_char;
		} else {
			return $str;
		}
	}
	
	public static function getVersion($title){
		if(empty($title))
			return false;
	}
}